package net.wanho.systemSettings.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Menu;
import net.wanho.systemSettings.po.Position;
import net.wanho.systemSettings.service.MenuService;
import net.wanho.systemSettings.service.PositionService;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/EmmPositionServlet")
public class EmmPositionServlet extends BaseServlet<PositionService> {
	private MenuService menuService = BeanFactory.getBean(MenuService.class) ;
	protected String getAllEmmPosition(HttpServletRequest request,HttpServletResponse response) throws ServletException{
			//1.获得请求参数【模糊查询条件】
			String positionname = request.getParameter("positionname");
			//2.创建map 封装查询条件
			Map<String,String> condMap = new HashMap<>(1) ;
			//3. 判断条件是否有效【有效】
			if (StringUtils.isNotEmpty(positionname)){
				//map中存放值   key ： SQL 列名   value  用户输入关键字
				condMap.put("POSITION_NAME",positionname) ;
			}
			List<Position> positionList = super.baseService.findByCond(condMap);
			//查询所有菜单
		List<Menu> parentMenuList = menuService.findByCond(new HashMap<String, String>());
		request.setAttribute("positionList",positionList);
		request.setAttribute("parentMenuList",parentMenuList);
			return  "/WEB-INF/view/systemSettings/position/index.jsp";
	}

	protected String addPosition(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		try {
			// 如果请求方式为 GET   实现页面跳转 显示新建界面
			if ("GET".equals(request.getMethod())){
				return "/WEB-INF/view/systemSettings/position/create.jsp" ;
			}
			// POST 请求 实现新增功能
			Position position = super.parseRequest(request, Position.class);
			super.baseService.save(position) ;
			return "redirect:/EmmPositionServlet?method=getAllEmmPosition";
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	
	protected AjaxResult positionNameCheck(HttpServletRequest request,HttpServletResponse response) {
		try {
			String positionName = request.getParameter("positionName");
			String positionId = request.getParameter("positionId");
			//职位id 为空 【新增界面  修改界面 都需要进行职位唯一性的验证】
			//新增界面 职位验证时  id 肯定是不存在的
			//修改界面 职位验证时  id 肯定是存在的
			if (StringUtils.isNotEmpty(positionId)){
				// bug [业务流程]  修改职位 必须修改职位名称 【霸王条款 捆绑销售？  比如：只修改职位等级？】
				Position position = super.baseService.findById(positionId);
				//职位名称不做修改
				if (position.getPositionName().equals(positionName)){
					//验证通过
					return AjaxResult.success(0) ;
				}
			}
			long count = super.baseService.count(positionName);
			if (count>0){
				return AjaxResult.fail() ;
			}
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.fail() ;
		}
	}
	protected AjaxResult deletePostionCheck(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		try {
			//1. 获得要删除的职位 的id
			String positionId = request.getParameter("positionId");
			//2. 根据要删除的职位id 进行 对 员工表  职位与菜单关系表 关联数据统计
			long cut = super.baseService.countPositionIdRef(positionId);
			//3. 数据统计结果 >0  [有关联数据  禁止删除]
			if (cut>0){
				return AjaxResult.fail("有关联数据，无法删除") ;
			}
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	
	
	protected String deletePosition(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			//1.获得要删除职位的id
			String positionId = request.getParameter("positionId");
			//2.调用业务方法进行删除操作
			super.baseService.remove(positionId) ;
			return "redirect:/EmmPositionServlet?method=getAllEmmPosition";
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	
	protected String getUpdatePostionDetailById(HttpServletRequest request,HttpServletResponse response) throws ServletException{
		try {
			String id = request.getParameter("id");
			Position position = super.baseService.findById(id);
			request.setAttribute("position",position);
			return "/WEB-INF/view/systemSettings/position/update.jsp" ;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	
	protected String updatePosition(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			//1. 获得 请求参数  封装到 pojo 实体类中
			Position position = super.parseRequest(request, Position.class);
			//2. 调用 修改职位的业务方法
			super.baseService.modify(position) ;
			return "redirect:/EmmPositionServlet?method=getAllEmmPosition";
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage()) ;
		}
	}
	//根据职位id 查询关联菜单的id集合
	protected AjaxResult getLinkPostionDetailById(HttpServletRequest request,HttpServletResponse response) throws ServletException {
		try {
			//获得职位id
			String positionId = request.getParameter("positionId");
			List<Integer> menuIdListByPostionId = menuService.findMenuIdListByPostionId(positionId);
			//Mock 数据 模拟数据  打桩数据
			//List<Integer> list = Arrays.asList(9,11,12,13) ;
			return AjaxResult.success(menuIdListByPostionId) ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.fail("查询关联菜单异常！") ;
		}
	}

	//关联 菜单
	protected AjaxResult positionLinks(HttpServletRequest request,HttpServletResponse response){
		try {
			//获得职位id
			String positionId = request.getParameter("positionId");
			String menuIds = request.getParameter("menuIds");
			//使用 ， 分割
			String[] menuIdArray = menuIds.split(",");
			//转换 menu对象数组
			List<Menu> menuList = new ArrayList<>(menuIdArray.length) ;
			for (String menuId : menuIdArray) {
				menuList.add(new Menu(Integer.parseInt(menuId))) ;
			}
			//封装 Postion对象
			Position position = new Position();
			position.setPositionId(Integer.parseInt(positionId));
			position.setMenuList(menuList);
			super.baseService.modifyPositionIdRef(position) ;
			return AjaxResult.success() ;
		} catch (ServiceException e) {
			e.printStackTrace();
			return AjaxResult.fail("关联菜单失败!") ;
		}
	}
}
